/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hybris;

import java.awt.Color;
import java.awt.Graphics;

/**
 *
 * @author menderleit
 */
public class Particle {

    private long duration;      // Lifetime
    private long startTime;     // When was it created?
    private double x, y;        // Position
    private double xv, yv;      // Velocity
    private double gravity;     // Self explanatory? :D
    private boolean active;     // show it?
    private double alpha;
    private double alphaFade;
    private int alphaMax;
    private int red, green, blue;
    private int size;
    private int sizeTime;
    private int sizeSpeed;
    private int sizeGrow;
    
    public Particle(int x, int y, double xv, double yv, double gravity, long duration, Color col) {
        startTime = System.nanoTime() / 1000000;
        this.duration = duration;
        this.gravity = gravity;
        this.x = x;
        this.y = y;
        this.xv = xv;
        this.yv = yv;
        alphaMax = 95;
        alpha = 1.0;
        alphaFade = 1.0 / (double) duration;
        red = col.getRed();
        green = col.getGreen();
        blue = col.getBlue();
        size = 25;
        sizeSpeed = 20;
        sizeTime = sizeSpeed;
        sizeGrow = 3;
        active = true;
    }

    public void setAlphaMax(int alphaMax) {
        this.alphaMax = alphaMax;
    }
    
    public void setGravity(double gravity) {
        this.gravity = gravity;
    }

    public void setDuration(long duration) {
        this.duration = duration;
    }

    public void setColor(Color col) {
        red = col.getRed();
        green = col.getGreen();
        blue = col.getBlue();
    }

    public void setSize(int size) {
        this.size = size;
    }

    public void setSizeSpeed(int sizeSpeed) {
        this.sizeSpeed = sizeSpeed;
        sizeTime = sizeSpeed;
    }

    public void setSizeGrow(int sizeGrow) {
        this.sizeGrow = sizeGrow;
    }
    
    public void update(long difTime) {
        if (active) {
            if (System.nanoTime() / 1000000 - startTime > duration) {
                active = false;
            }
            x += xv;
            y += yv;
            yv += gravity;
            alpha -= alphaFade * difTime;
            if (alpha < 0.0) {
                alpha = 0.0;
            }
            sizeTime -= difTime;
            while (sizeTime <= 0) {
                sizeTime += sizeSpeed;
                size += sizeGrow;
            }
        }
    }

    public void draw(Graphics g, int screenWidth, int screenHeight) {
        if (active) {
            int SW = screenWidth;
            int SH = screenHeight;
            if (x >= 0 - size && x <= SW && y >= 0 - size && y <= SH) {
                g.setColor(new Color(red, green, blue, (int) (alpha * (double) alphaMax)));
                g.fillOval((int) x, (int) y, size, size);
            }
        }
    }

    public boolean isActive() {
        return active;
    }
}
